<template>
	<view class="content">
		<view class="background-img" v-if="backgroundImg">
			<uv-image :src="backgroundImg" width="750rpx" mode="widthFix"></uv-image>
		</view>
		<view class="main" v-if="codeList.length>0">
			<view class="code-content">
				<template v-for="(item, index) in codeList">
					<view class="code-item" v-if="item.url" :key="index" @click="viewItem(item)">
						<image :src="item.url" class="code_img" mode=""></image>
						<text class="code-title" v-if="item.link">点击或长按识别二维码</text>
						<text class="code-title" v-else>长按识别二维码</text>
					</view>
				</template>
			</view>
		</view>
		<template v-else>
			<uv-empty icon="empty-order"></uv-empty>
		</template>
	</view>

</template>

<script setup>
	import {
		ref
	} from 'vue';
	import {
		onLoad,
		onShow
	} from '@dcloudio/uni-app';
	import {
		$Api
	} from '@/api/index.js';
	let codeName = ['qrcode_one', 'qrcode_two', 'qrcode_three'];
	let codeTitle = {
		'qrcode_one': 'qrcode_one_url',
		'qrcode_two': 'qrcode_two_url',
		'qrcode_three': 'qrcode_three_url'
	};
	onLoad((option) => {
		if (option.verify) {
			getQrcode(option.verify);
		} else {
			uni.showToast({
				title: '缺少参数',
				icon: 'none'
			})
		}
	})
	onShow(() => {
		// var ua = navigator.userAgent.toLowerCase();
		// var isWeixin = ua.indexOf('micromessenger') != -1;
		// if (!isWeixin) {
		// 	window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize"
		// }
	})

	const codeList = ref([]);
	const backgroundImg = ref('');
	const getQrcode = async (verify) => {
		const res = await $Api.user.getQrcode({
			// verify: 'JuqK17423a'
			verify
		}, true);
		let codeArr = [];
		if (res.code == 1 && res.data) {
			backgroundImg.value = res.data.background ? res.data.background : '';
			for (let key in res.data) {
				if (typeof(res.data[key]) === 'string') {
					if (codeName.includes(key)) {
						let linkText = '';
						if (res.data.extend && codeTitle[key]) {
							linkText = res.data.extend[codeTitle[key]] ? res.data.extend[codeTitle[key]] : '';
						}
						codeArr.push({
							link: linkText,
							url: res.data[key]
						});
					}
				}
			}
			codeList.value = codeArr;
			console.log('codeArr', codeArr);
		} else {
			uni.showToast({
				title: res.msg,
				icon: 'none'
			})
		}
	}
	const viewItem = (item) => {
		if (item && item.link) {
			window.location.href = item.link;
		}
	}
</script>
<style lang="scss" scoped>
	.content {
		width: 100%;
		height: 100vh;
		position: relative;
		background: transparent;
		// border: 1rpx solid transparent;
		box-sizing: border-box;
		overflow: hidden;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
	}

	.background-img {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		z-index: -1;
	}

	.main {
		border-radius: 12rpx;
		background-color: #f5f5f5;
		padding: 20rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		// margin: 0 auto;
		.tips {
			font-size: 36rpx;
			font-weight: 600;
			color: #333;
		}

		.tips-box {
			height: 48rpx;
			border-radius: 24rpx;
			margin-bottom: 12rpx;
			// background: #A57F5C;
			padding: 0 24rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}

	.code-content {
		width: 580rpx;
		box-sizing: border-box;
		padding: 12rpx;
		border-radius: 12rpx;
		// display: grid;
		// row-gap: 20rpx;
		// column-gap: 20rpx;
		// grid-template-columns: repeat(2, 1fr);
		// // margin-top: 320rpx;
		background-color: rgba(255, 255, 255, 0.5);
		display: flex;
		flex-wrap: wrap;
		justify-content: center;
		align-items: center;
		gap: 20rpx;
		// backdrop-filter: blur(20rpx);
		// -webkit-backdrop-filter: blur(20rpx);
		// backdrop-filter: blur(20rpx);
		// -webkit-backdrop-filter: blur(20rpx);



		.code-item {
			width: 260rpx;
			box-sizing: border-box;
			display: flex;
			justify-content: center;
			align-items: center;
			flex-direction: column;

			.code-title {
				font-size: 24rpx;
				font-weight: 600;
				color: #333333;
				margin-top: 10rpx;
			}


		}

		.code_img {
			width: 100%;
			height: 260rpx;
			border-radius: 8rpx;
		}
	}
</style>